package com.example.leetcode.greedy.common;

import java.util.Arrays;

public class GreedyUtils {


    public static int maxArea(int[] height) {
        if (height == null || height.length < 2) {
            return -1;
        }
        if (height.length == 2) {
            return Math.min(height[0], height[1]);
        }
        int result = 0;
        for (int i = 0; i < height.length - 1; i++) {
            for (int j = height.length - 1; j > i; j--) {
                int width = j - i;
                if (height[j] >= height[i]) {
                    result = Math.max(result, width * height[i]);
                    break;
                } else {
                    result = Math.max(result, width * height[j]);
                }
            }
        }
        return result;
    }

    public static String getSmallestString(int n, int k) {
        char[] str = new char[n];
        for (int i = 0; i < n; i++) {
            str[i] = 'a';
        }
        int rest = k - n;
        int index = n - 1;
        while (rest > 0) {
            if (rest >= 25) {
                str[index] = 'z';
                rest -= 25;
                index--;
            } else {
                str[index] = (char) (str[index] + rest);
                rest = 0;
            }
        }
        return new String(str);
    }

    public static int getMaximumConsecutive(int[] coins) {
        int res = 1;
        Arrays.sort(coins);
        for (int i : coins) {
            if (i > res) {
                break;
            }
            res += i;
        }
        return res;
    }

    public static int fillCups(int[] amount) {
        Arrays.sort(amount);
        if (amount[0] + amount[1] <= amount[2]) {
            return amount[2];
        } else {
            int extra = amount[0] + amount[1] - amount[2];
            if (extra % 2 == 0) {
                return amount[2] + extra / 2;
            } else {
                return amount[2] + extra / 2 + 1;
            }
        }
    }
}
